package com.miplus.ccrm.core.service.commission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.commission.AgentModeEditVo;
import com.miplus.ccrm.core.controller.vo.commission.AgentModeReqVo;
import com.miplus.ccrm.core.controller.vo.commission.AgentModeResVo;
import com.miplus.ccrm.core.pojo.FiCommissionAgentMode;
import com.miplus.ccrm.core.pojo.FiCommissionTemplate;
import com.miplus.ccrm.core.service.commission.FiCommissionAgentModeService;
import com.miplus.ccrm.core.mapper.FiCommissionAgentModeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author wangzl
* @description 针对表【fi_commission_agent_mode(催员计算模式表)】的数据库操作Service实现
* @createDate 2025-03-01 07:06:11
*/
@Service
public class FiCommissionAgentModeServiceImpl extends MPJBaseServiceImpl<FiCommissionAgentModeMapper, FiCommissionAgentMode>
    implements FiCommissionAgentModeService{
    @Override
    public IPage<AgentModeResVo> getAgentModeList(AgentModeReqVo para)
    {
        return selectJoinListPage(
                new Page<>(para.getPageNum(),para.getPageSize()),
                AgentModeResVo.class,
                new MPJLambdaWrapper<FiCommissionAgentMode>()
                        .eq(StringUtils.isNotEmpty(para.getDisttag()),FiCommissionAgentMode::getDisttag,para.getDisttag())
                        .eq(ObjectUtils.isNotNull(para.getAgentmode()),FiCommissionAgentMode::getAgentmode,para.getAgentmode())
                        .selectAll(FiCommissionAgentMode.class)
                        .selectAs("fun_getnames('SORTING.QUEUE',t.disttag)", AgentModeResVo::getDisttagDesc)
                        .selectAs("fun_getnames('COMMISSION.AGENTMODE',t.agentmode)",AgentModeResVo::getAgentmodeDesc )
                );
    }
    @Override
    public CmdResVo addAgentMode(String usrId, FiCommissionAgentMode fiCommissionAgentMode)
    {
        fiCommissionAgentMode.setModeid(null);
        fiCommissionAgentMode.setCrdt(new Date());
        fiCommissionAgentMode.setCrusr(usrId);
        save(fiCommissionAgentMode);
        return new CmdResVo(1,fiCommissionAgentMode.getModeid().toString());
    }
    @Override
    public
    CmdResVo updateAgentMode(String usrId, AgentModeEditVo para)
    {
        if(ObjectUtils.isNull(para.getModeid()))
        {
            return new CmdResVo(-1, "参数错误");
        }
        FiCommissionAgentMode fiCommissionAgentMode = new FiCommissionAgentMode();
        BeanUtils.copyProperties(para,fiCommissionAgentMode);
        fiCommissionAgentMode.setMddt(new Date());
        fiCommissionAgentMode.setMdusr(usrId);
        updateById(fiCommissionAgentMode);
        return new CmdResVo(1,"更新成功");
    }
    @Override
    public
    CmdResVo deleteAgentMode(Integer modeId)
    {
        if(ObjectUtils.isNull(modeId))
        {
            return new CmdResVo(-1, "参数错误");
        }
        removeById(modeId);
        return new CmdResVo(1,"删除成功");
    }
    @Override
    public FiCommissionAgentMode getAgentModeByDisttag(String disttag, String startDate)
    {
        return baseMapper.selectOne( new QueryWrapper<FiCommissionAgentMode>()
                .eq("disttag",disttag));
    }
}




